/*
 * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 * The software in this package is published under the terms of the CPAL v1.0
 * license, a copy of which has been included with this distribution in the
 * LICENSE.txt file.
 */
package org.mule.runtime.core.lifecycle;

import org.mule.runtime.core.api.MuleContext;
import org.mule.runtime.core.api.MuleRuntimeException;
import org.mule.runtime.core.api.context.notification.ServerNotification;
import org.mule.runtime.core.config.i18n.CoreMessages;
import org.mule.runtime.core.context.notification.MuleContextNotification;
import org.mule.runtime.core.util.ClassUtils;

import java.lang.reflect.Constructor;

/**
 * TODO
 */
public class NotificationLifecycleObject extends LifecycleObject {

  private String preNotificationName;
  private String postNotificationName;
  private Constructor ctor;

  public NotificationLifecycleObject(Class type) {
    super(type);
  }

  public NotificationLifecycleObject(Class type, Class notificationClass) {
    super(type);

    if (notificationClass == null) {
      throw new IllegalArgumentException(CoreMessages.objectIsNull("notificationClass").toString());
    }

    // MULE-2903: make sure the notifiactionClass is properly loaded and initialized
    notificationClass = ClassUtils.initializeClass(notificationClass);

    if (!ServerNotification.class.isAssignableFrom(notificationClass)) {
      throw new ClassCastException("Notification class must be of type: " + ServerNotification.class.getName()
          + ". Offending class is: " + notificationClass.getName());
    }

    ctor = ClassUtils.getConstructor(notificationClass, new Class[] {Object.class, String.class});
    if (ctor == null) {
      throw new IllegalArgumentException("No constructor defined in Notification class: " + notificationClass
          + " with arguments (Object.class, String.class)");
    }
  }

  public NotificationLifecycleObject(Class type, Class notificationClass, int preNotification, int postNotification) {
    this(type, notificationClass);
    setPreNotificationName(MuleContextNotification.getActionName(preNotification));
    setPostNotificationName(MuleContextNotification.getActionName(postNotification));
  }

  public String getPostNotificationName() {
    return postNotificationName;
  }

  public void setPostNotificationName(String postNotificationName) {
    this.postNotificationName = postNotificationName;
  }

  public String getPreNotificationName() {
    return preNotificationName;
  }

  public void setPreNotificationName(String preNotificationName) {
    this.preNotificationName = preNotificationName;
  }

  @Override
  public void firePreNotification(MuleContext context) {
    if (getPreNotificationName() != null) {
      setPreNotification(createNotification(context, getPreNotificationName()));
    }
    super.firePreNotification(context);

  }

  @Override
  public void firePostNotification(MuleContext context) {
    if (getPostNotificationName() != null) {
      setPostNotification(createNotification(context, getPostNotificationName()));
    }
    super.firePostNotification(context);
  }

  protected ServerNotification createNotification(MuleContext context, String action) {
    try {
      return (ServerNotification) ctor.newInstance(context, action);
    } catch (Exception e) {
      throw new MuleRuntimeException(CoreMessages.failedToCreate("Notification:" + action), e);
    }
  }
}
